package com.cy.核心编程.thead.basic.yellow;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 读取数据的生产者
 * <p>
 * 知识点：private static AtomicInteger i = new AtomicInteger(); 线程安全的数字
 * i.incrementAndGet()  -->  相当于 i++
 */
public class ReadVideoThread extends Thread {
    //线程安全
    private static AtomicInteger i = new AtomicInteger();

    //准备能装视频的缓冲区
    private BlockingQueue<Video> videoQueue;

    public ReadVideoThread(BlockingQueue<Video> videoQueue) {
        this.videoQueue = videoQueue;
    }

    @Override
    public void run() {
        while (true) {
            String name = "苍老师" + i.incrementAndGet();
            Video v = new Video(name);

            try {
                videoQueue.put(v);
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("发现了一个视频：" + name);
        }
    }
}
